package com.example.myapplication;

import android.os.Build;
import android.util.ArraySet;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2020/12/15
 * @description
 */
public class QueryJson2 {
    private static volatile List<DataList> dataList = new ArrayList<>();
    private static volatile int indexClass;
    private static volatile int idnexJuzi;
    static List<Query1.Bean> list;
    static ExecutorService pool;

    public static void main(String[] args) {
        list = new Query1().init();
        pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            nextClass();
        }
    }

    static volatile boolean isNexPage = true;

    public static void nextClass() {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                if (indexClass < list.size()) {
                    Query1.Bean bean = list.get(indexClass);
                    indexClass++;
                    System.out.println("index " + indexClass);
                    new Query2().init(bean);
                }
            }
        });

        if (dataList.size() == list.size() && isNexPage) {
            isNexPage = false;
            nextPage();
        }
    }


    static Query3.DataJson dataJson;
    static List<Query3.DataJson> dataJsonList = new ArrayList<>();

    public static void saveFile(Query3.DataJson dataJson) {
        File file = new File("D:\\wrf\\qn\\" + dataJson.jsonName + ".json");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            String json = GsonUtil.toJson(dataJson);
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(json);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void nextPage() {
        if (dataJson != null) {
            saveFile(dataJson);
            dataJsonList.add(dataJson);
        }

        if (idnexJuzi < dataList.size()) {
            DataList data = dataList.get(idnexJuzi);
            dataJson = new Query3.DataJson();
            dataJson.name = data.name;
            dataJson.jsonName = data.jsonName;
            idnexJuzi++;
            System.out.println(data.name + " 页数 " + idnexJuzi);
            nextJuzi(data);
        }


        if (dataJsonList.size() == dataList.size()) {
            System.out.println("最终大小 " + dataJsonList.size());
        }

    }


    public static void nextJuzi(DataList data) {
        List<String> dataList = new ArrayList<>(data.urls);
        for (int i = 0; i < 10; i++) {
            nextJuzi2(data, dataList);
        }
    }

    public static void nextJuzi2(DataList data, List<String> dataList) {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                if (data.index < dataList.size()) {
                    String url = dataList.get(data.index);
                    data.index++;
                    System.out.println(data.name + " 条数 " + data.index);
                    new Query3(data, dataList).getDns(url);
                }
            }
        });

        if (data.index == dataList.size() && data.isNext) {
            data.isNext = false;
            nextPage();
        }
    }


    public static class DataList {
        public String name;
        public String jsonName;
        public HashSet<String> urls;
        public int index;
        public boolean isNext = true;

        public DataList(String name, String jsonName, HashSet<String> urls) {
            this.name = name;
            this.jsonName = jsonName;
            this.urls = urls;
        }
    }

    public static class Query1 {
        List<Bean> list = new ArrayList<>();


        public List<Bean> init() {
            list.add(new Bean("weimei", "唯美的句子"));
            list.add(new Bean("zheli", "哲理的句子"));
            list.add(new Bean("aiqing", "爱情的句子"));
            list.add(new Bean("xinsui", "心碎的句子"));
            list.add(new Bean("xinfan", "心烦的句子"));
            list.add(new Bean("anlian", "暗恋的句子"));
            list.add(new Bean("shiwang", "失望的句子"));
            list.add(new Bean("xiangni", "想你的句子"));
            list.add(new Bean("maren", "骂人的句子"));
            list.add(new Bean("shuqing", "抒情的句子"));
            list.add(new Bean("youmei", "优美的句子"));
            list.add(new Bean("lizhi", "励志的句子"));
            list.add(new Bean("youqing", "友情的句子"));
            list.add(new Bean("xinsuan", "心酸的句子"));
            list.add(new Bean("xinlei", "心累的句子"));
            list.add(new Bean("shilian", "失恋的句子"));
            list.add(new Bean("wunai", "无奈的句子"));
            list.add(new Bean("xiangjia", "想家的句子"));
            list.add(new Bean("gaoxiao", "搞笑的句子"));
            list.add(new Bean("shiyi", "诗意的句子"));
            list.add(new Bean("shanggan", "伤感的句子"));
            list.add(new Bean("jingdian", "经典的句子"));
            list.add(new Bean("qinqing", "亲情的句子"));
            list.add(new Bean("xintong", "心痛的句子"));
            list.add(new Bean("xinjing", "心静的句子"));
            list.add(new Bean("biaobai", "表白的句子"));
            list.add(new Bean("nanguo", "难过的句子"));
            list.add(new Bean("sinian", "思念的句子"));
            list.add(new Bean("xianshi", "现实的句子"));
            list.add(new Bean("huiyi", "回忆的句子"));
            return list;
        }

        public static class Bean {
            public String name;
            public String url;

            public Bean(String url, String name) {
                this.url = url;
                this.name = name;
            }
        }
    }


    public static class Query2 {
        HashSet<String> urlList = new HashSet<>();
        List<String> baseUrl = new ArrayList<>();
        String url;
        int index = -1;
        Query1.Bean bean;
        String host = "https://www.diyijuzi.com/";

        void init(Query1.Bean bean) {
            this.bean = bean;
            this.url = host + bean.url;
            getDns(url);
        }

        public void getDns(String url) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                    if (response.body() != null) {
                        String html = response.body().string();
                        if (baseUrl.isEmpty()) {
                            int size = getNumber(html);
                            baseUrl.add(url);
                            for (int i = 2; i < size + 2; i++) {
                                baseUrl.add(url + "/list_" + i + ".html");
                            }
                        }
                        nextSubUrl(html, url);
                        index++;
                        if (index < baseUrl.size()) {
                            getDns(baseUrl.get(index));
                        } else {
                            dataList.add(new DataList(bean.name, bean.url, urlList));
                            nextClass();
                        }
                    }
                }
            });
        }

        public int getNumber(String html) {
            String s1 = "<a class=\"a1\">";
            int index = html.indexOf(s1);
            String subHtml = html.substring(index);
            int last = subHtml.indexOf("条");
            String num = subHtml.substring(s1.length(), last);
            int size = (int) Math.ceil(Integer.parseInt(num) / 10);
            return size;
        }

        public void nextSubUrl(String html, String cla) {
            int index = html.indexOf(cla);
            if (index > 0) {
                String subHtml = html.substring(index);
                int last = subHtml.indexOf("html") + 4;
                String url = subHtml.substring(0, last);

                char nextChar = subHtml.charAt(cla.length() + 1);
                if (Character.isDigit(nextChar)) {
                    urlList.add(url);
                }
                nextSubUrl(subHtml.substring(last), cla);
            }
        }
    }


    public static class Query3 {
        DataList data;
        List<String> dataList;
        DataJson2 dataJson2 = new DataJson2();

        public Query3(DataList data, List<String> dataList) {
            this.data = data;
            this.dataList = dataList;
        }

        public void getDns(String url) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }

                @Override
                public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                    if (response.body() != null) {
                        String html = response.body().string();
                        getTitle(html);
                        nextSub(html);
                        dataJson.article.add(dataJson2);
                        nextJuzi2(data, dataList);
                    }
                }
            });
        }

        public void nextSub(String html) {
            int index = html.indexOf("<p>") + 3;
            char nextChar = html.charAt(index);
            int last = html.indexOf("</p>");
            if (Character.isDigit(nextChar)) {
                String result = html.substring(index, last);
                dataJson2.strings.add(result);
            }
            if (index > 3) {
                String nextHtml = html.substring(last + 4);
                nextSub(nextHtml);
            }
        }


        public void getTitle(String html) {
            String tag = "headline\":\"";
            int index = html.indexOf(tag);
            String subHtml = html.substring(index);
            int last = subHtml.indexOf("\",");
            String result = subHtml.substring(tag.length(), last);
            dataJson2.title = result;
        }

        public static class DataJson {
            String name;
            String jsonName;
            List<DataJson2> article = new ArrayList<>();
        }

        public static class DataJson2 {
            String title;
            List<String> strings = new ArrayList<>();
        }

    }
}
